{% extends "base.html" %}

{% block title %}Package files {% if project %} - {{ project }}{% endif %}{% endblock title %}

{% block page_content %}
<table id="table" class="table table-bordered table-consensed table-striped" cellspacing="0" width="100%">
  <div>
      <button id="del_btn" type="button" class="btn btn-danger" disabled="disabled" style="margin-bottom:15px" data-toggle="modal" data-target="#myModal" >删除</button>
 </div>
  
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
			<div class="modal-body">
			     Are you sure to delete the selected files?
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Cancel
				</button>
				<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="delete_files()">
				   OK	
				</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
  <thead>
  <tr>
    <th><input type="checkbox" id="selectall-checkbox" onclick="checkAll(this)"></th>
    <th>文件名</th>
    <th>文件大小</th>
    <th>创建时间</th>
  </tr>
  </thead>

<tbody>

{% if files %}
{% for item in files %}
<tr>
  <td><input id="select-checkbox" type="checkbox" value="{% if project %}{{ project }}{% if prosub %}/{{ prosub }}{% endif %}{% endif %}/{{ item.name }}" onclick="checkOne(this)"></td>
  <td><a href="/jenkins{% if project %}/{{ project }}{% if prosub %}/{{ prosub }}{% endif %}{% endif %}/{{ item.name }}">{{ item.name }}</a></td>
  <td>{{ item.size }}</td>
  <td>{{ item.time }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
  <td> No files listed </td>
  <td></td>
  <td></td>
  <td></td>
</tr>
{% endif %} 
</tbody>
</table>
{% endblock %}


{% block scripts %}
{{ super() }}
<script>
    function checkAll(obj){
        $("#table tbody tr input[type='checkbox']").prop('checked', $(obj).prop('checked'));
        if($(obj).prop('checked'))
        {
            $("#del_btn").prop('disabled',false);
        }
        if(retrive_data().length == 0)
        {
            $("#del_btn").prop('disabled',true);
        }
    }

    function checkOne(obj){
        $("#selectall-checkbox").prop('checked', false);
        if($(obj).prop('checked'))
        {
            $("#del_btn").prop('disabled',false);
        }
        if(retrive_data().length == 0)
        {
            $("#del_btn").prop('disabled',true);
            $("#download_btn").prop('disabled',true);
        }
    }


    function retrive_data(){
        var data = [];
        $("#table").find(":checkbox:checked").each(function(){
            obj = $(this).parent().next();
           // base_uri = $(obj)["0"].firstChild.baseURI;
            val = $(this)["0"].value;    
            var pos = data.indexOf(val);
            if(pos == -1 && val != "文件名") data.push(val);
        });
        console.log(data.toString());
        return data;
    }
    
    function delete_files(){
        var obj;
        console.log(retrive_data());  
        obj=$.ajax({
            type: 'POST',
            url: '/delete',
            async: true,
	    dataType: "text",
            data: {"fileList":retrive_data()},    
	    success: function (data, textStatus, jqXHR) {
                window.location.reload();
                console.log(textStatus);
      		},
      	    error: function(){
        	console.log("Failed to delete files!");
            }
        });
    }


</script>

{% endblock %}

